@model List<CaipuGuanliXitong.Models.Ingredient>

@{
    ViewData["Title"] = "食材管理";
}

<h2>食材管理</h2>

@if (ViewBag.Message != null)
{
    <div class="alert @(ViewBag.IsSuccess ? "alert-success" : "alert-danger")">
        @ViewBag.Message
    </div>
}

<div class="row mb-3">
    <div class="col-md-6">
        <a asp-action="Create" class="btn btn-primary">新增食材</a>
        <a asp-action="ExportToExcel" class="btn btn-success">导出Excel</a>
    </div>
    <div class="col-md-6">
        <form asp-action="ImportFromExcel" method="post" enctype="multipart/form-data" class="form-inline justify-content-end">
            <div class="form-group">
                <input type="file" name="file" class="form-control-file" accept=".xlsx,.xls" />
            </div>
            <button type="submit" class="btn btn-info ml-2">导入Excel</button>
        </form>
    </div>
</div>

<form asp-action="Index" method="get">
    <div class="form-actions no-color">
        <p>
            搜索: <input type="text" name="SearchString" value="@ViewBag.CurrentFilter" />
            <input type="submit" value="搜索" class="btn btn-default" /> |
            <a asp-action="Index">返回完整列表</a>
        </p>
    </div>
</form>

<table class="table">
    <thead>
        <tr>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.IdSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">编号</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.CategorySortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">类别</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.NameSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">食材名称</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.SpecificationSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">规格</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.UnitSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">单位</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.PriceSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">单价</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.RemarkSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">备注</a>
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Category)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Specification)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Unit)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Remark)
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-info btn-sm">编辑</a>
                    <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-success btn-sm">详情</a>
                    <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-sm">删除</a>
                </td>
            </tr>
        }
    </tbody>
</table>

<div>
    @{
        var totalItems = ViewBag.TotalItems != null ? (int)ViewBag.TotalItems : Model.Count();
        var currentPage = ViewBag.CurrentPage != null ? (int)ViewBag.CurrentPage : 1;
        var pageSize = ViewBag.PageSize != null ? (int)ViewBag.PageSize : 10;
        var totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
        
        // 计算当前页的起始和结束项目编号
        var startItem = (currentPage - 1) * pageSize + 1;
        var endItem = Math.Min(currentPage * pageSize, totalItems);

        <div class="row mb-3">
            <div class="col-md-6">
                <div class="pagination-info">
                    显示第 <strong>@startItem</strong> 到 <strong>@endItem</strong> 项，共 <strong>@totalItems</strong> 项
                </div>
            </div>
            <div class="col-md-6">
                @if (totalPages > 1)
                {
                    <nav aria-label="Page navigation" class="d-flex justify-content-end">
                        <ul class="pagination">
                            @if (currentPage > 1)
                            {
                                <li class="page-item">
                                    <a class="page-link" asp-action="Index" asp-route-page="1" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort" aria-label="First">
                                        <span aria-hidden="true">首页</span>
                                    </a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" asp-action="Index" asp-route-page="@(currentPage - 1)" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                            }

                            @{
                                // 计算要显示的页码范围
                                int startPage = Math.Max(1, currentPage - 2);
                                int endPage = Math.Min(totalPages, currentPage + 2);
                                
                                // 确保始终显示5个页码（如果 totalPages >= 5）
                                if (endPage - startPage < 4)
                                {
                                    if (startPage == 1)
                                    {
                                        endPage = Math.Min(totalPages, startPage + 4);
                                    }
                                    else
                                    {
                                        startPage = Math.Max(1, endPage - 4);
                                    }
                                }
                            }

                            @if (startPage > 1)
                            {
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                            }

                            @for (var i = startPage; i <= endPage; i++)
                            {
                                <li class="page-item @(i == currentPage ? "active" : "")">
                                    <a class="page-link" asp-action="Index" asp-route-page="@i" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort">@i</a>
                                </li>
                            }

                            @if (endPage < totalPages)
                            {
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                            }

                            @if (currentPage < totalPages)
                            {
                                <li class="page-item">
                                    <a class="page-link" asp-action="Index" asp-route-page="@(currentPage + 1)" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" asp-action="Index" asp-route-page="@totalPages" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort" aria-label="Last">
                                        <span aria-hidden="true">末页</span>
                                    </a>
                                </li>
                            }
                        </ul>
                    </nav>
                }
            </div>
        </div>
    }
</div>